home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / kaffe / object.c < prev    next >
C/C++ Source or Header  |  1996-02-15  |  3KB  |  161 lines

  1. /*
  2.  * object.c
  3.  * Handle create and subsequent garbage collection of objects.
  4.  *
  5.  * Copyright (c) 1996 Systems Architecture Research Centre,
  6.  *           City University, London, UK.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <assert.h>
  16. #include "gtypes.h"
  17. #include "itypes.h"
  18. #include "object.h"
  19. #include "classMethod.h"
  20. #include "baseClasses.h"
  21. #include "gc.h"
  22. #include "errors.h"
  23.  
  24. /*
  25.  * Primitive object allocation.
  26.  */
  27. inline
  28. object*
  29. newObject(int sz)
  30. {
  31.     object* obj;
  32.  
  33.     obj = (object*)calloc(sz, sizeof(char));
  34.     if (obj == 0) {
  35.         throwException(OutOfMemoryError);
  36.     }
  37.     return (obj);
  38. }
  39.  
  40. /*
  41.  * Create a new object based on the given class type.
  42.  */
  43. object*
  44. alloc_object(classes* c, bool perm)
  45. {
  46.     object* obj;
  47.  
  48.     obj = newObject(sizeof(object) + c->fsize * sizeof(u4));
  49.  
  50.     obj->type = c;
  51.     obj->size = c->fsize;
  52.     obj->mtable = c->mtable;
  53.     obj->final = c->final == true ? 0 : 1;
  54.  
  55.     add_object(obj, perm);
  56.  
  57.     return (obj);
  58. }
  59.  
  60. /*
  61.  * Create a new class object.
  62.  */
  63. classes*
  64. alloc_class(void)
  65. {
  66.     classes* obj;
  67.  
  68.     obj = (classes*)newObject(sizeof(classes));
  69.  
  70.     obj->final = 1;
  71.     if (ClassClass != 0) {
  72.         obj->head.type = ClassClass;
  73.         obj->head.mtable = ClassClass->mtable;
  74.         obj->head.size = ClassClass->fsize;
  75.     }
  76.  
  77.     add_object(&obj->head, true);
  78.  
  79.     return (obj);
  80. }
  81.  
  82. /*
  83.  * Create a new array of data items (not objects).
  84.  */
  85. object*
  86. alloc_array(int sz, int type)
  87. {
  88.     object* obj;
  89.     int elemsz;
  90.     classes* class;
  91.  
  92.     assert(type < MAXTYPES);
  93.     assert(TYPE_SIZE(type) != 0);
  94.  
  95. assert(sz < 100000);
  96.  
  97.     obj = newObject(sizeof(object) + sz*TYPE_SIZE(type));
  98.  
  99.     class = lookupArray(TYPE_ARRAYSIG(type));
  100.     obj->type = class;
  101.     obj->size = sz;
  102.     obj->mtable = class->mtable;
  103.     obj->final = 1;
  104.  
  105.     add_object(obj, false);
  106.  
  107.     return (obj);
  108. }
  109.  
  110. /*
  111.  * Create a new array of objects.
  112.  */
  113. object*
  114. alloc_objectarray(int sz, char* csig)
  115. {
  116.     char sig[MAXSIG];
  117.     object* obj;
  118.     classes* class;
  119.  
  120. assert(sz < 100000);
  121.  
  122.     obj = (object*)newObject(sizeof(object) + sz*sizeof(u4));
  123.  
  124.     /* Build signature for array type */
  125.     strcpy(sig, "[");
  126.     strcat(sig, csig);
  127.  
  128.     class = lookupArray(addString(sig));
  129.     obj->type = class;
  130.     obj->size = sz;
  131.     obj->mtable = class->mtable;
  132.     obj->final = 1;
  133.  
  134.     add_object(obj, false);
  135.  
  136.     return (obj);
  137. }
  138.  
  139. /*
  140.  * Build a multi-dimensional array.
  141.  */
  142. object*
  143. alloc_multiarray(int* dims, char* cname)
  144. {
  145.     object* obj;
  146.     object** array;
  147.     int i;
  148.  
  149.     obj = alloc_objectarray(dims[0], cname+1);
  150.     if (dims[1] > 0) {
  151.         array = (object**)obj->data;
  152.         for (i = 0; i < dims[0]; i++) {
  153.             array[i] = alloc_multiarray(dims+1, cname+1);
  154.         }
  155.     }
  156.  
  157.     add_object(obj, false);
  158.  
  159.     return (obj);
  160. }
  161.